package com.qiaofang.applet.rest;

import com.qiaofang.applet.common.dto.DataRequest;
import com.qiaofang.applet.common.dto.DataResponse;
import com.qiaofang.applet.common.dto.PageDataRequest;
import com.qiaofang.applet.common.exception.BusinessException;
import com.qiaofang.applet.common.util.PageUtil;
import com.qiaofang.applet.constants.UpDownFlagConstants;
import com.qiaofang.applet.service.other.domain.Topic;
import com.qiaofang.applet.service.other.service.TopicService;
import com.qiaofang.applet.service.other.service.dto.TopicDetailDto;
import com.qiaofang.applet.service.other.service.mapstruct.TopicMapper;
import com.qiaofang.applet.service.property.domain.FirsthandEstate;
import com.qiaofang.applet.service.property.domain.Property;
import com.qiaofang.applet.service.property.service.FirsthandEstateService;
import com.qiaofang.applet.service.property.service.PropertyService;
import com.qiaofang.applet.service.property.service.dto.*;
import com.qiaofang.applet.service.property.service.mapstruct.FirsthandEstateMapper;
import com.qiaofang.applet.service.property.service.mapstruct.PropertyMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
 * @author daiyuxaing
 */
@RestController
@RequestMapping("/api/web/topic")
@Api(tags = "房源：主题房模块")
public class TopicController {

    @Autowired
    private TopicService topicService;

    @Autowired
    private PropertyService propertyService;

    @Autowired
    private FirsthandEstateService firsthandEstateService;

    @Autowired
    private TopicMapper topicMapper;

    @Autowired
    private PropertyMapper propertyMapper;

    @Autowired
    private FirsthandEstateMapper firsthandEstateMapper;

    @PostMapping("/searchTopic")
    @ApiOperation(value = "首页主题", notes = "首页主题")
    public DataResponse<List<TopicDetailDto>> searchTopic(@RequestBody @Valid DataRequest<CityQueryDto> request) {
        return DataResponse.ok(topicMapper.domainToDetailDto(topicService.getIndexTopic(request.getParam())));
    }

    @PostMapping("/getTopic")
    @ApiOperation(value = "主题详情", notes = "主题详情")
    public DataResponse<TopicDetailDto> getTopic(@RequestBody @Valid DataRequest<IdDto> request) {
        TopicDetailDto result = topicService.getById(request.getParam().getId());

        return DataResponse.ok(result);
    }

    @PostMapping("/searchFirsthandTopicProperty")
    @ApiOperation(value = "查询新房主题房源列表", notes = "查询新房主题房源列表")
    public DataResponse<Page<FirsthandEstateListDto>> searchFirsthandTopicProperty(@RequestBody @Valid PageDataRequest<FirsthandEstateQueryDto> request) {
        String topicId = request.getParam().getTopicId();
        if (StringUtils.isBlank(topicId)) {
            throw new BusinessException("请先选择主题");
        }

        Optional<Topic> topic = topicService.findById(topicId);

        List<String> uuidList = null;
        if (topic.isPresent()) {
            Set<String> uniqueUuidSet = topic.get().getUniqueUuids();
            if (uniqueUuidSet != null) {
                uuidList = new ArrayList<>(uniqueUuidSet);
            }
        }

        Page<FirsthandEstateListDto> result = null;

        if (!CollectionUtils.isEmpty(uuidList)) {
            request.getParam().setEstateUniqueUuids(uuidList);
            //主题房增加上架筛选
            request.getParam().setUpDownFlag(UpDownFlagConstants.UP_FLAG.getValue());
            Page<FirsthandEstate> firsthandEstatePage = firsthandEstateService.findAllByParams(request.getParam(), PageUtil.getPageable(request));

            result = firsthandEstateMapper.convertDomainToListDto(firsthandEstatePage);
        }

        return DataResponse.ok(result);
    }

    @PostMapping("/searchSaleTopicProperty")
    @ApiOperation(value = "查询二手房主题房源列表", notes = "查询二手房主题房源列表")
    public DataResponse<Page<SalePropertyListDto>> searchSaleTopicProperty(@RequestBody @Valid PageDataRequest<SalePropertyQueryDto> request) {
        String topicId = request.getParam().getTopicId();
        if (StringUtils.isBlank(topicId)) {
            throw new BusinessException("请先选择主题");
        }

        Optional<Topic> topic = topicService.findById(topicId);

        List<String> uuidList = null;
        if (topic.isPresent()) {
            Set<String> uniqueUuidSet = topic.get().getUniqueUuids();
            if (uniqueUuidSet != null) {
                uuidList = new ArrayList<>(uniqueUuidSet);
            }
        }

        Page<SalePropertyListDto> result = null;

        if (!CollectionUtils.isEmpty(uuidList)) {
            request.getParam().setPropertyUniqueUuids(uuidList);
            //主题房增加上架筛选
            request.getParam().setUpDownFlag(UpDownFlagConstants.UP_FLAG.getValue());

            Page<Property> propertyPage = propertyService.findAllSaleByParams(request.getParam(), PageUtil.getPageable(request));

            result = propertyMapper.convertDomainToSaleListDto(propertyPage);
        }

        return DataResponse.ok(result);
    }

    @PostMapping("/searchRentTopicProperty")
    @ApiOperation(value = "查询租房主题房源列表", notes = "查询租房主题房源列表")
    public DataResponse<Page<RentPropertyListDto>> searchRentTopicProperty(@RequestBody @Valid PageDataRequest<RentPropertyQueryDto> request) {
        String topicId = request.getParam().getTopicId();
        if (StringUtils.isBlank(topicId)) {
            throw new BusinessException("请先选择主题");
        }

        Optional<Topic> topic = topicService.findById(topicId);

        List<String> uuidList = null;
        if (topic.isPresent()) {
            Set<String> uniqueUuidSet = topic.get().getUniqueUuids();
            if (uniqueUuidSet != null) {
                uuidList = new ArrayList<>(uniqueUuidSet);
            }
        }

        Page<RentPropertyListDto> result = null;

        if (!CollectionUtils.isEmpty(uuidList)) {
            request.getParam().setPropertyUniqueUuids(uuidList);
            //主题房增加上架筛选
            request.getParam().setUpDownFlag(UpDownFlagConstants.UP_FLAG.getValue());
            Page<Property> propertyPage = propertyService.findAllRentByParams(request.getParam(), PageUtil.getPageable(request));

            result = propertyMapper.convertDomainToRentListDto(propertyPage);
        }

        return DataResponse.ok(result);
    }

}

